﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyaWo.Entity;
using Commen;
using DAL;

namespace WinService
{
    public class PanGuFenCi
    {
        public void ToDo()
        {
            try
            {
                SpiderContentService ctx = new SpiderContentService();
                List<SpiderContent> lsc = ctx.GetListFenCis();

                if (lsc == null || lsc.Count() == 0) { return; }

                foreach (var tyq in lsc)
                {
                    string ID = tyq.ID;
                    if (ID.IsNullOrWhiteSpace())
                        return;
                    string key1 = tyq.key1;
                    string key2 = tyq.key2;
                    string key3 = tyq.key3;
                    string key4 = tyq.key4;
                    string key5 = tyq.key5;
                    if (key1.NotNullOrWhiteSpace() || key2.NotNullOrWhiteSpace() || key3.NotNullOrWhiteSpace() || key4.NotNullOrWhiteSpace() || key5.NotNullOrWhiteSpace())
                        return;
                    var q = tyq;
                    FenCi(q.Title, ref q);

                    key1 = q.key1;
                    key2 = q.key2;
                    key3 = q.key3;
                    key4 = q.key4;
                    key5 = q.key5;

                    if (key1.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key1); ctx.Upk1v1(ID, kv); }
                    if (key2.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key2); ctx.Upk2v2(ID, kv); }
                    if (key3.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key3); ctx.Upk3v3(ID, kv); }
                    if (key4.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key4); ctx.Upk4v4(ID, kv); }
                    if (key5.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key5); ctx.Upk5v5(ID, kv); }
                    if (key1.NotNullOrWhiteSpace() || key2.NotNullOrWhiteSpace() || key3.NotNullOrWhiteSpace() || key4.NotNullOrWhiteSpace() || key5.NotNullOrWhiteSpace())
                    {
                        ctx.UpHaso(ID, key1,   key2,   key3,   key4,   key5);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("ToDofenci ： " + ex.Message);
            }
        }

        void FenCi(string title, ref SpiderContent sc)
        {
            try
            {
                if (title.IsNullOrWhiteSpace())
                {
                    return;
                }
                PanGu.Segment.Init();
                PanGu.Segment segment = new PanGu.Segment();
                ICollection<PanGu.WordInfo> words = segment.DoSegment(title);
                if (words == null || words.Count() == 0)
                {
                    return;
                }
                var q = words.OrderByDescending(x => x.Frequency).Take(5).ToArray();
                if (q[0] != null)
                {
                    sc.key1 = q[0].Word;
                }
                if (q[1] != null)
                {
                    sc.key2 = q[1].Word;
                }
                if (q[2] != null)
                {
                    sc.key3 = q[2].Word;
                }
                if (q[3] != null)
                {
                    sc.key4 = q[3].Word;
                }
                if (q[4] != null)
                {
                    sc.key5 = q[4].Word;
                }
            }
            catch (Exception ex)
            {
                Log.Error("fenci ： " + ex.Message);
            }
        }

        /// <summary>
        /// 在线查询关键字的排名情况
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public KeyValueModel GetKeyValue(string keyName)
        {
            KeyValueModel kv = new KeyValueModel();
            try
            {
                try
                {
                    var docText = HtmlHelper.PostHtmlContent("http://s.tool.chinaz.com/baidu/words.aspx", $"kw={WebHelper.UrlEncode(keyName)}&page=&by=0&pn=", "utf-8");
                    if (docText.IsNullOrWhiteSpace()) { kv.value = 0; return kv; }
                    //加载源代码，获取文档对象
                    var doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(docText);
                    string key = string.Empty;
                    string value = string.Empty;
                    if (doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[1]/p[1]/a[1]/span[1]") != null)
                    {
                        key = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[1]/p[1]/a[1]/span[1]").InnerText;
                    }
                    else
                    {
                        key = string.Empty;
                    }
                    if (doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[2]/a") != null)
                    {
                        value = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[2]/a").InnerText;
                    }
                    else
                    {
                       value = "0";
                    }

                    kv.KeyName = key;
                    if (value == null) value = "0";
                    kv.value = value.ToInt();
                    return kv;
                }
                catch (Exception ex)
                {
                    Log.Error(keyName);
                    Log.Error(ex.Message);
                    kv.value = 0;
                    return kv;
                }
            }
            catch (Exception ex)
            {
                Log.Error("GetKeyValue fenci ： " + ex.Message);
                return kv;
            }

        }
    }
}
